import request from '@/utils/request'
import { BaseResponse } from '@/types/api'

// 菜单相关接口的类型定义
export interface Menu {
  id: number;
  parentId: number;
  name: string;
  title: string;
  icon?: string;
  path: string;
  component?: string;
  redirect?: string | { path: string };
  sort: number;
  hidden: number;  // 0: 显示, 1: 隐藏, 2: 隐藏
  status: number;
  activeMenu?: string;
  noCache: number;  // 1: 不缓存, 2: 缓存
  alwaysShow: number;  // 1: 忽略, 2: 不忽略
  breadcrumb: number;  // 1: 可见, 2: 隐藏
  children?: Menu[];
}

export interface MenuTree extends Menu {
  children?: MenuTree[];
}

export interface MenuData {
  parentId: number;
  name: string;
  title: string;
  icon?: string;
  path: string;
  component?: string;
  redirect?: string;
  sort: number;
  hidden: boolean;
  status: number;
  activeMenu?: string;
  noCache?: boolean;
}

// 获取菜单树
export function getMenuTree(): Promise<BaseResponse<MenuTree[]>> {
  return request({
    url: '/menu/tree',
    method: 'get'
  })
}

// 获取菜单列表
export function getMenuList(): Promise<BaseResponse<Menu[]>> {
  return request({
    url: '/menu/list',
    method: 'get'
  })
}

// 创建菜单
export function createMenu(data: MenuData): Promise<BaseResponse> {
  return request({
    url: '/menu/create',
    method: 'post',
    data
  })
}

// 更新菜单
export function updateMenuById(menuId: number, data: Partial<MenuData>): Promise<BaseResponse> {
  return request({
    url: '/menu/update/' + menuId,
    method: 'patch',
    data
  })
}

// 批量删除菜单
export function batchDeleteMenuByIds(data: { menuIds: number[] }): Promise<BaseResponse> {
  return request({
    url: '/menu/delete/batch',
    method: 'delete',
    data
  })
}

// 获取用户的可访问菜单列表
export function getUserMenusByUserId(Id: number): Promise<BaseResponse<Menu[]>> {
  return request({
    url: '/menu/access/list/' + Id,
    method: 'get'
  })
}

// 获取用户的可访问菜单树
export function getUserMenuTreeByUserId(Id: number): Promise<BaseResponse<MenuTree[]>> {
  return request({
    url: '/menu/access/tree/' + Id,
    method: 'get'
  })
}
